function AJRequest()
{
	this.dest = null;
	this.url = null;
	this.params = null;
	this.loading = false;
	this.response = "";

	if(window.XMLHttpRequest)
	{
		this.xmlhttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	// funzione di callback
	this.onStateChange = function(e)
	{
		if(this.readyState == 4)
		{
			this.parent.loading = false;
			if(this.status == 200)
			{
				if(this.responseText == "fail")
				{
					var err = "page: "+this.parent.url+"\n"+
						"params: "+this.parent.params+"\n"+
						"è stato ritornato un messaggio di fail. verificare!";
					if(this.parent.dest!=null)this.parent.dest.innerHTML = err;
					this.parent.response = err;
					//this.abort();
					if(this.parent.callback!=null)this.parent.callback("fail");
				}
				else
				{
					if(this.parent.dest!=null)
						this.parent.dest.innerHTML = this.responseText;
					this.parent.response = this.responseText;
					//this.abort();
					if(this.parent.callback!=null)
						this.parent.callback(this.responseText);
				}
			}
		}
	}
	// converte l'oggetto parametro {par1:val,par2:val} in parametri HTTP
	// ad es "par1=val&par2=val&aj
	// il parametro aj valida la provenienza ajax della richiesta
	this.toParams = function(obj)
	{
		var out = "";
		for(o in obj)
			out += o+"="+obj[o]+"&";
		return out+"aj";
	}
	// carica il contenuto della request all'interno dell'oggetto richiesto
	this.loadIntoHTML = function(objdest, url, params_obj)
	{
		this.url = url;
		this.callback = null;
		this.params = this.toParams(params_obj);
		this.dest = objdest;
		//this.dest.innerHTML = "loading...";
		this.reqPage();
	}
	// carica il contenuto della request all'interno dell'oggetto richiesto
	this.loadIntoVar = function(url, params_obj, callback)
	{
		this.url = url;
		this.callback = callback;
		this.params = this.toParams(params_obj);
		this.dest = null;
		this.reqPage();
	}
	this.reqPage = function()
	{
		if(this.loading) return;
		this.loading = true;
		this.response = "";
		this.xmlhttp.abort();
		this.xmlhttp.parent = this;
		this.xmlhttp.onload = this.onStateChange;
		this.xmlhttp.onreadystatechange = this.onStateChange;
		this.xmlhttp.open("POST", this.url, true);
		this.xmlhttp.setRequestHeader('Content-Type',
			'application/x-www-form-urlencoded; charset=UTF-8');
		this.xmlhttp.send(this.params);
	}
}
